package net.shopxx.action.shop;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.ServletContext;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;


@SuppressWarnings("serial")
public class UploadAction extends BaseShopAction {

	
	public String userphoto(){
	
		try{
			HttpServletRequest request = ServletActionContext.getRequest();

			
			String userid = request.getParameter("userid") + "";
			System.out.println("userid:" + userid);
			int n,total=0; 
			
			if (userid == null || userid.equals("")) { userid = "temp"; }
			
			String path = "userphoto/" + userid + ".jpg";
			String realPath = ServletActionContext.getServletContext().getRealPath(path);
			
			ServletInputStream in = request.getInputStream();
			BufferedInputStream fileIn = new BufferedInputStream(in);
			
			File file = new File(realPath);
			File parent = file.getParentFile();
			if (!parent.exists()){ parent.mkdirs();}
			
			FileOutputStream out = new FileOutputStream(file); 
			byte[] b=new byte[1024]; 
			
			while((n=fileIn.read(b))> 0){ 
				total = total + n;
				out.write(b, 0, n); 
			}
			
			System.out.println("total:" + total);
			
			out.close(); 
			fileIn.close(); 
			in.close(); 
	
			
			//System.out.println("detail:" + in);
			//System.out.println("Length:" + request.getContentLength());
			//System.out.println("ContentType:" + request.getContentType());
			//System.out.println("File length total:" + total);
			
			
	    
		}catch(Exception ex){ 
			ex.printStackTrace();
		}
		return "userphoto";
		
	}




	
}
